package com.tjetc.service.impl;

import com.tjetc.domain.Customer;
import com.tjetc.mapper.CustomerMapper;
import com.tjetc.service.CustomerService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

import java.util.Objects;

public class CustomerServiceImpl implements CustomerService {
    private CustomerMapper customerMapper= Db.getMapper(CustomerMapper.class);

    /**
     * 客户登录
     * @param customer
     * @return
     */
    @Override
    public Result login(Customer customer) {
        Customer customer1 = customerMapper.get(customer.getAccount(), customer.getPassword());
        if (customer1 != null) {
            return new Result(200, "登陆成功", customer1);
        } else {
            return new Result(500, "登陆失败", null);
        }
    }

    /**
     * 客户注册
     * @param customer
     * @return
     */
    @Override
    public Result register(Customer customer) {
        Customer customer1=customerMapper.get(customer.getAccount(),null);
        if (customer1==null){
            int rows=customerMapper.insert(customer);
            if (rows==1){
                return new Result(200,"添加成功",null);
            }
            else {
                return new Result(500,"添加失败",null);
            }
        }
        return new Result(500,"该用户名已存在",null);
    }

    /**
     * 客户列表
     * @return
     */
    @Override
    public Result list() {
        return new Result(200,null,customerMapper.list());
    }

    /**
     * 更新客户
     * @param customer
     * @return
     */
    @Override
    public Result update(Customer customer) {
        Customer customer1=customerMapper.selectByPrimaryKey(customer.getCustomerId());
        if (customer1==null){
            return new Result(500,"查无此人",null);
        }else {
            String account = customer.getAccount();
            Customer customer2=customerMapper.selectByAccount(account);
            if (customer2!=null&&customer2.getCustomerId()!=customer.getCustomerId()){
                return new Result(500,"已有该账户!",null);
            }
            int rows=customerMapper.updateByPrimaryKey(customer);
                if (rows==1){
                    return new Result(200,"修改成功",null);
            }
        }
        return new Result(500,"修改失败",null);
    }

    /**
     * 根据id删除客户
     * @param id
     * @return
     */
    @Override
    public Result deleteById(int id) {
        Customer customer=customerMapper.selectByPrimaryKey(id);
        if (customer==null){
            return new Result(500,"查无此人",null);
        }else {
            int rows=customerMapper.deleteByPrimaryKey(id);
            if (rows==1){
                return new Result(200,"删除成功",null);
            }
        }
        return new Result(500,"删除失败",null);
    }

    @Override
    public Result selectById(int id) {
        Customer customer1=customerMapper.selectByPrimaryKey(id);
        if (customer1==null){
            return new Result(500,"查无此人",null);
        }else {
            return new Result(200,"查询成功",customer1);
        }
    }
}
